<?php
/**
 * Created by 784855684@qq.com.
 * User: linzh
 * Date: 7/31/17
 * Time: 9:20 AM
 */


namespace inframe\test\core\database;

use inframe\core\database\LiteDB;
use inframe\test\Wsunit;
use inframe\throws\db\SqliteException;

class SQLiteTest extends Wsunit
{
    /**
     * @var LiteDB
     */
    private $db;

    public function testCreateDB()
    {
        try {
            new LiteDB('db4test');
            $this->wrongHere();
        } catch (SqliteException $exception) {
            $this->rightHere();
        }
        $db = new LiteDB('db4test', true);
        $this->assertTrue(is_file($db->getDatabasePath()));
        $this->rightHere();
        return $this->db = $db;
    }

    /**
     * @param LiteDB $db
     * @depends testCreateDB
     */
    public function testgettables(LiteDB $db)
    {
        $this->assertTrue(count($db->getTables()) === 2);
        $this->assertTrue($db->hasTable('lite'));
        $this->assertTrue($db->hasTable('sqlite_sequence'));# 当SQLite数据库中包含自增列时,会自动建立一个名为 sqlite_sequence 的表
    }

    /**
     * @param LiteDB $db
     * @depends testCreateDB
     * @return LiteDB
     */
    public function testcreatetable(LiteDB $db)
    {
        try {
            $db->createTable(LiteDemoModel::class);
            $this->rightHere();
            $this->assertTrue(count($db->getTables()) === 3);
            $this->assertTrue($db->hasTable('lite'));
            $this->assertTrue($db->hasTable('tb4test'));
            $this->assertTrue($db->hasTable('sqlite_sequence'));
            return $db;
        } catch (SqliteException $exception) {
            echo $exception->getMessage() . "\n";
            $this->wrongHere();
            return null;
        }
    }

    /**
     * @depends testcreatetable
     * @param LiteDB $db
     * @return LiteDemoModel
     */
    public function testinsert(LiteDB $db)
    {
        /** @var LiteDemoModel $model */
        $model = $db->getModel(LiteDemoModel::class);
        $this->assertEquals(LiteDemoModel::class, get_class($model));
        $model->K1 = 12;
        $model->K2 = 'hello';
        $this->assertEquals(1, $model->create());
        $model->K1 = 'hello';
        $model->K2 = 'world';
        $this->assertEquals(1, $model->create());

        $list = $model->select();
        $this->assertEquals(1, $list[0]['ID']);
        $this->assertEquals(12, $list[0]['K1']);
        $this->assertEquals('hello', $list[0]['K2']);
        $this->assertEquals(2, $list[1]['ID']);
        $this->assertEquals('hello', $list[1]['K1']);
        $this->assertEquals('world', $list[1]['K2']);
        return $model;
    }

    /**
     * @depends testinsert
     * @param LiteDemoModel $model
     * @return LiteDemoModel
     */
    public function testfind(LiteDemoModel $model)
    {
        $data = $model->find(1);
        $this->assertEquals(1, $data['ID']);
        $this->assertEquals(12, $data['K1']);
        $this->assertEquals('hello', $data['K2']);
        $this->assertEquals(1, $model->ID);
        $this->assertEquals(12, $model->K1);
        $this->assertEquals('hello', $model->K2);
        return $model;
    }

    /**
     * @param LiteDemoModel $model
     * @depends testfind
     */
    public function testupdate(LiteDemoModel $model)
    {
        $model->K1 = 'jsd';
        $model->K2 = 'kaka';
        $this->assertTrue(1 === $model->update());
        $data = $model->find(1);
        $this->assertTrue('jsd' === $data['K1']);
        $this->assertTrue('kaka' === $data['K2']);
    }

    public function __destruct()
    {
        $this->db and unlink($this->db->getDatabasePath());
    }

//    /**
//     * 测试连接
//     */
//    public function testConnect()
//    {
//        try {
//            $this->assertTrue(is_file(SQLite::getSqlite()));
//            $this->assertTrue(is_executable(SQLite::getSqlite()));
//            SQLite::getInstance('dbnotexist');
//            $this->wrongHere();
//        } catch (FileNotFoundException $exception) {
//            $this->rightHere();
//        }
//    }
//
//    /**
//     * 测试生成数据库
//     */
//    public function testCreate()
//    {
//        try {
//            SQLite::install('db4test');
//            $this->rightHere();
//        } catch (FileExistException $exception) {
//            $this->wrongHere();
//        } catch (DatabaseException $exception) {
//            $this->wrongHere();
//        }
//        # 数据库存在提示文件存在
//        try {
//            SQLite::install('db4test');
//            $this->wrongHere();
//        } catch (FileExistException $exception) {
//            $this->rightHere();
//        } catch (FileNotFoundException $exception) {
//            $this->wrongHere();
//        } finally {
//            unlink(IN_PATH_DATA . 'dblite/db4test.db');
//        }
//    }
//
//
//    public function testInstance()
//    {
//        try {
//            $lite = SQLite::getInstance('db4test2', true);
//
//        } catch (FileNotFoundException $exception) {
//
//        }
//    }

}